function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}




//添加finally：因为还有一个参数里面还有一个complete方法。

Promise.prototype.finally = function(callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => {
      throw reason
    })
  );
};

//封装异步api
const wxPromisify = fn => {
  return function(obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function(res) {
        resolve(res)
      }
      obj.fail = function(res) {
        reject(res)
      }
      fn(obj)
    })
  }
}

const getLocationPromisified = wxPromisify(wx.getLocation); //获取经纬度
const showModalPromisified = wxPromisify(wx.showModal); //弹窗

// 无需token
const request = (data) => {
  wx.showLoading({
    title: '加载中...'
  })
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: data.url,
      data: data.data,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success(res) { //服务器返回数据
        if (res.data.errcode == '00001') {

          wx.clearStorageSync()
          wx.redirectTo({
            url: '/pages/login/index',
          })

        }
        if (res.data.errcode == '00002' || res.data.errcode == '00003') {
          return wx.showToast({
            title: res.data.errmsg,
            icon: 'none',
            duration: 3000
          })
        }
        resolve(res.data);
      },
      fail(err) {
        reject(err);
      },
      complete(e) {
        wx.hideLoading()
      }
    })
  });
  return promise;
}


// 封装token请求
const requestToken = (data) => {
  wx.showLoading({
    title: '加载中...'
  })
  var promise = new Promise((resolve, reject) => {
    //网络请求
    wx.request({
      url: data.url,
      data: data.data,
      method: data.method || 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token'),
      },
      success(res) { //服务器返回数据
        if (res.data.errcode == '00001') {
          wx.clearStorageSync()
          return wx.showToast({
            title: '请登录后体验',
            icon: 'none'
          })
     
        }
        if (res.data.errcode == '00002' || res.data.errcode == '00003') {
          return wx.showToast({
            title: res.data.errmsg,
            icon: 'none',
            duration: 3000
          })
        }
        wx.hideLoading()
        resolve(res.data);
      },
      fail(err) {
        reject(err);
      },

    })
  });
  return promise;
}

module.exports = {
  request,
  getLocationPromisified,
  showModalPromisified,
  requestToken,
  formatTime: formatTime
}